#!/bin/sh

#-------------------------------------#
#  SGT (Software Generation Toolkit)  #
#-------------------------------------#
#                                     #
#      Build Visual C++ project       #
#       without dependencies          #
#      for the current library        #
#                                     #
#  GDE 04/09/2003 v2.4                #
#       create rte directory          #
#       if it doesn't exist           #
#                                     #
#  GDE 24/07/2003 v2.3                #
#       add -fopt option              #
#                                     #
#  GDE 03/07/2003 v2.2                #
#       check \MAK subdirectory       #
#                                     #
#  GDE 11/03/2003 v2.1                #
#       Correct the OPTION_DSP=ALL    #
#                                     #
#  GDE 06/01/2003 v2.0                #
#       Modify the script for         #
#       Open-MMI usage                #
#                                     #
#  GDE 25/09/2002 v1.3                #
#       update for SGT 1.1.4          #
#                                     #
#  GDE 07/05/2002 v1.2                #
#       update for SGT 1.1.0          #
#                                     #
#  GDE 25/03/2002 v1.1                #
#       update the version            #
#        and help sections            #
#                                     #
#  GDE 07/02/2002 v1.0                #
#       creation                      #
#                                     #
#-------------------------------------#
# $1: target / specific options       #
#-------------------------------------#



#-------------------------------------#
# Script informations
#-------------------------------------#
export SCRIPT_SGT_VER=2.4
export SCRIPT_SGT_DATE=04/09/2003
export SCRIPT_SGT_USER=GDE
export SCRIPT_SGT_NAME=make_rte
export SCRIPT_SGT_USAGE="Build a library or a DSP file for Remote Task Environment (RTE)"
#-------------------------------------#



#---------------------
# Create and go to output directory
#---------------------
if [ -d mak ]
then
	mkdir -p -m775 rte
	mkdir -p -m775 out
	cd out
else
	export MSG_ERROR_SCRIPT=0
	export CMD_SCRIPT=Error
fi
#---------------------



#-------------------------------------#
# Parameters section
#-------------------------------------#
#-------------------------------------#
# Default parameters
#-------------------------------------#
export OPTION_MAK=USE_ONLY_STACK_FOR_RTE=OFF
export OPTION_MAK2=USE_FILE_OPTION=OFF


#---------------------
# Parameter 5 -> additional options (used with `-rte or -dsp`)
#---------------------
if [ ! -z $5 ] && [ -z $CMD_SCRIPT ]
then
	if [ $5 = -stack ]
	then
		export OPTION_MAK=USE_ONLY_STACK_FOR_RTE=ON
	else
		if [ $5 = -fopt ]
		then
			export OPTION_MAK2=USE_FILE_OPTION=ON
		else
			export MSG_ERROR_SCRIPT=2
			export CMD_SCRIPT=Error
		fi
	fi
fi

#---------------------
# Parameter 4 -> additional options (used with `-rte or -dsp`)
#---------------------
if [ ! -z $4 ] && [ -z $CMD_SCRIPT ]
then
	if [ $4 = -stack ]
	then
		export OPTION_MAK=USE_ONLY_STACK_FOR_RTE=ON
	else
		if [ $4 = -fopt ]
		then
			export OPTION_MAK2=USE_FILE_OPTION=ON
		else
			export MSG_ERROR_SCRIPT=2
			export CMD_SCRIPT=Error
		fi
	fi
fi

#---------------------
# Parameter 3 -> additional options
#---------------------
if [ ! -z $3 ] && [ -z $CMD_SCRIPT ]
then
	if [ $3 = -stack ]
	then
		export OPTION_MAK=USE_ONLY_STACK_FOR_RTE=ON
	else
		if [ $3 = -fopt ]
		then
			export OPTION_MAK2=USE_FILE_OPTION=ON
		else
			if [ $3 = -debug ]
			then
				export OPTION_DSP=DEBUG
			else
				if [ $3 = -release ]
				then
					export OPTION_DSP=RELEASE
				else
					if [ $3 = -all ]
					then
						export OPTION_DSP=ALL
					else
							export MSG_ERROR_SCRIPT=2
							export CMD_SCRIPT=Error
					fi
				fi
			fi
		fi
	fi
fi


#---------------------
# Parameter 2 -> other options
#---------------------
if [ ! -z $2 ] && [ -z $CMD_SCRIPT ]
then
	if [ ! $2 = -dsp ] && [ ! $2 = -build ] && [ ! $2 = -rebuild ] && [ ! $2 = -clean ] && [ ! $2 = -rte ]
	then
		export MSG_ERROR_SCRIPT=2
		export CMD_SCRIPT=Error
	fi
fi

#---------------------
# Parameter 1 -> target makefile / specific option
#---------------------
if [ ! -z $1 ] && [ -z $CMD_SCRIPT ]
then
	if [ $1 = -v ]
	then
		export CMD_SCRIPT=Version
	else
		if [ $1 = -h ]
		then
			export CMD_SCRIPT=Help
		else
			if [ $1 = --help ]
			then
				export CMD_SCRIPT=Help
			else
				if [ $1 = -hm ]
				then
					export CMD_SCRIPT=More
				else
					if [ $1 = -u ]
					then
						export CMD_SCRIPT=Usage
					else
						if [ -f ../mak/$1.mak ]
						then
							export TARGET_DSP=$1
						else
							export MSG_ERROR_SCRIPT=1
							export CMD_SCRIPT=Error
						fi
					fi
				fi
			fi
		fi
	fi
fi


#-------------------------------------#
# if no option is defined, display help
#-------------------------------------#
if [ $# -eq 0 ] && [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Help
fi


#-------------------------------------#
# Process section
#-------------------------------------#
#---------------------
# Build DSP / DSW files
#---------------------
if [ -z $CMD_SCRIPT ] && [ ! -z $1 ] && [ ! -z $2 ]
then
	echo " "
	echo "    $SCRIPT_SGT_USAGE"
	echo " "

	if [ $2 = -dsp ] && [ ! -z $TARGET_DSP ]
	then
		make -f ../mak/gen.mak TARGET=$TARGET_DSP REMOTETASKS=ON $OPTION_MAK $OPTION_MAK2 RTE_RULE=make_dsp manage_rte
		exit
	fi
	
	if [ ! -z $3 ]
	then
		if [ $2 = -build ] && [ ! -z $TARGET_DSP ]
		then
			make -f ../mak/gen.mak TARGET=$TARGET_DSP REMOTETASKS=ON MSDEV_OPT=$OPTION_DSP MSDEV_OPT2=/BUILD RTE_RULE=build_lib_rte manage_rte
			exit
		fi
	
		if [ $2 = -rebuild ] && [ ! -z $TARGET_DSP ]
		then
			make -f ../mak/gen.mak TARGET=$TARGET_DSP REMOTETASKS=ON MSDEV_OPT=$OPTION_DSP MSDEV_OPT2=/REBUILD RTE_RULE=build_lib_rte manage_rte
			exit
		fi
	
		if [ $2 = -clean ] && [ ! -z $TARGET_DSP ]
		then
			make -f ../mak/gen.mak TARGET=$TARGET_DSP REMOTETASKS=ON MSDEV_OPT=$OPTION_DSP MSDEV_OPT2=/CLEAN RTE_RULE=build_lib_rte manage_rte
			exit
		fi

		if [ $2 = -rte ] && [ ! -z $TARGET_DSP ]
		then
			make -f ../mak/gen.mak TARGET=$TARGET_DSP REMOTETASKS=ON $OPTION_MAK $OPTION_MAK2 RTE_RULE=make_dsp manage_rte
			make -f ../mak/gen.mak TARGET=$TARGET_DSP REMOTETASKS=ON MSDEV_OPT=$OPTION_DSP MSDEV_OPT2=/REBUILD RTE_RULE=build_lib_rte manage_rte
			exit
		fi
	else
		export MSG_ERROR_SCRIPT=3
		export CMD_SCRIPT=Error
	fi
fi



#-------------------------------------#
# Help script section
#-------------------------------------#
if [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Version
fi

#---------------------
# Usage
#---------------------
if [ $CMD_SCRIPT = Usage ]
then
	echo "  $SCRIPT_SGT_NAME	$SCRIPT_SGT_USAGE"
	exit
fi

#---------------------
# Version
#---------------------
echo " "
echo "-------------------------------------"
echo " '$SCRIPT_SGT_NAME' (release "$SCRIPT_SGT_VER") for SGT "$SGT_VER". Last update by "$SCRIPT_SGT_USER" on "$SCRIPT_SGT_DATE
echo "  $SCRIPT_SGT_USAGE"
if [ $CMD_SCRIPT = Version ]
then
	echo " "
	exit
fi

#---------------------
# Simple help
#---------------------
if [ $CMD_SCRIPT = Help ]
then
	echo "-------------------------------------"
	echo " $SCRIPT_SGT_NAME [TARGET] [-OPTION] [COMPILATION_OPTIONS]"
	echo " "
	echo "  [-OPTION] : "
	echo "  -dsp      Build the current visual C++ project for RTE"
	echo " "
	echo "  -build    Build the current library for RTE from TARGET.dsp"
	echo "  -rebuild  Clean and rebuild the current library for RTE from TARGET.dsp"
	echo "  -clean    Clean the current library for RTE from TARGET.dsp"
	echo " "
	echo "  -rte      Create the visual C++ project and build the current library for RTE"
	echo " "
	echo "  [COMPILATION_OPTIONS] : "
	echo "  -fopt     Set all DSP option (flags and resource paths) in ..\rte\option.rte"
	echo "  -stack    Set only the stack flags for the TARGET.dsp"
	echo " "
	echo "  -debug    Debug mode"
	echo "  -release  Release mode"
	echo "  -all      Debug and Release mode"
	echo "-------------------------------------"
	echo "  -h | --help  Display this help "
	echo "  -hm          Display more help about SGT" $SGT_VER
	echo "  -v           Display the version of the current script"
fi

#---------------------
# More help
#---------------------
if [ $CMD_SCRIPT = More ]
then
	make -f ../mak/gen.mak help USE_TARGET_FILE=OFF
fi



#-------------------------------------#
# Error messages section
#-------------------------------------#
if [ $CMD_SCRIPT = Error ]
then
	if [ $MSG_ERROR_SCRIPT = 0 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The root path is not a valid SGT directory. Please check for the \MAK sub-directory"
	fi
	if [ $MSG_ERROR_SCRIPT = 1 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : ../mak/$1.mak doesn't exist !"
	fi
	if [ $MSG_ERROR_SCRIPT = 2 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : unknown option. Check syntax use !"
	fi
	if [ $MSG_ERROR_SCRIPT = 3 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : You must specify the compilation mode. Check syntax use !"
	fi
	echo " "
	echo " Check your command line or see help section -> $SCRIPT_SGT_NAME -h"
fi
